package org.insidesource.flexbugs.dao.impl;

import java.util.List;

import org.insidesource.flexbugs.dao.IssueDao;
import org.insidesource.flexbugs.model.Issue;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

public class IssueDaoJdbcImpl extends SimpleJdbcDaoSupport implements IssueDao
{

	public Issue get(Long id)
	{
		String sql = "select * from issue where id=" + id;
		return this.getSimpleJdbcTemplate().query(sql, ParameterizedBeanPropertyRowMapper.newInstance(Issue.class)).get(0);
	}

	public List<Issue> getAll()
	{
		String sql = "select * from issue";
		return this.getSimpleJdbcTemplate().query(sql, ParameterizedBeanPropertyRowMapper.newInstance(Issue.class));
	}

	public void remove(Long id)
	{
		String sql = "delete from issue where id=" + id;
		this.getSimpleJdbcTemplate().update(sql);
	}

	public void save(Issue issue)
	{
		String sql = "insert into issue(project, description, type, severity, status, details, reportedBy, reportedOn, assignedTo, estimatedHours) values(?, ?, ?, ?, ?, ?, ?, now(), ?, ?)";
		
		this.getSimpleJdbcTemplate().update(sql, issue.getProject(), issue.getDescription(), issue.getType(), issue.getSeverity(), issue.getStatus(), issue.getDetails(), issue.getReportedBy(), issue.getAssignedTo(), issue.getEstimatedHours());
	}

}
